
<%= javascript_include_tag "prototype" %>

<ul id="question-list" style="margin-left:0px">

<% if @part.questions.size == 0 %>
  还没有题目
<% end %>

<% i = 0 %>
<% @part.questions.each do |q| %>
  <% i += 1 %>
<li id="item_<%=q.id%>" style="list-style-type:none">

<div id="show_item_<%=q.id%>">
  <table width="440" border="0" align="center" cellpadding="0" cellspacing="0">
     <tr>
      <td width="60%">
        第<%= i %>题.&nbsp;&nbsp;
        <%= q.description %>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="javascript:;" onclick="$('show_item_<%=q.id%>').hide(); $('edit_item_<%=q.id%>').show();">修改</a>
        <%= link_to_remote "删除", :update => "questionsDiv",
                :url => { :action => "destroy_question", :id => q }
        %>
        <!--a href="#" onclick="new Effect.Fold('item_<%=q.id%>',{duration:1.5})">hide</a-->
        <!--
        <a href="#" onclick="deleteQuestion(<%=q.id%>)">删除</a>
        -->
      </td>
      <td width="62">
      </td>
    </tr>
    
     <tr>
     <td>
       <!-- if q.number >该题题号是：<=q.number.to_s><end-->
      </td>
      <td width="62">
      </td>
     </tr>
     
    <!--选项-->
    <% q.choices.each do |c| %>
    <tr>
      <td><label>
        <input type="radio" name="RadioGroup1" value="单选"/>
        <%= c.content %> 
          
        </label>
      </td>
      <td class="red"><%= c.value %>分&nbsp;&nbsp;&nbsp;
      <a href="" onclick="destroy_choice(<%=c.id%>,<%=@part.survey.id%>)">删除</a></td>
    </tr>
    
    <% end %>
    <!--选项结束-->
    
    
    <tr><td height="18"></td></tr>
    
  </table>
</div>
<div id="edit_item_<%=q.id%>" style="display:none">

  <form id="editForm<%=q.id%>" name="editForm<%=q.id%>" action="/surveys/prof_surveys/update_question" method="post">
    <input type="hidden" name="test_id" value="<%=q.survey.id%>" />
    <input type="hidden" name="id" value="<%=q.id%>" />
    <input type="hidden" name="question_num" value="<%=i%>" />
  <table width="440" border="0" align="center" cellpadding="0" cellspacing="0" id="item_<%=q.id%>">
    <tr>
      <td width="" colspan="2">
        第<%= i %>题.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="button" value="保存"
               onclick="updateQuestion($('editForm<%=q.id%>'), <%=q.id%>, $('show_item_<%=q.id%>'), $('edit_item_<%=q.id%>'))" />
        <a href="#" onclick="$('edit_item_<%=q.id%>').hide(); $('show_item_<%=q.id%>').show();">取消</a>
        <br/>
        <textarea name="description" value="<%=q.description%>" cols="50"><%=q.description%></textarea>
      </td>
    </tr>
   <tr><td height="20"> 题号 ：<input type="text"  name="question_number" size="8" value="<%=q.number%>"></td></tr>
    <!--选项-->
    <% q.choices.each do |c| %>
    <input type="hidden" name="choice_ids[]" value="<%=c.id%>" />
    <tr>
      <td><label>
        选项
        <input type="text" name="choice<%=c.id%>[content]" value="<%= c.content %>"/>
        </label>
      </td>
      <td class="red">
        分值
        <input type="text" name="choice<%=c.id%>[value]" value="<%= c.value %>"/>
      </td>
    </tr>

    <% end %>
    <!--选项结束-->


    <tr><td height="18"></td></tr>
 
  </table>
  </form>
  <br/><br/>
</div>
</li>

<% end %> 
</ul>
<div><font height="18" color="red"><%=flash[:q_error]%></font></div>
<%= sortable_element 'question-list',  
    :url => { :action => "sort", :id => @part },
    :complete => visual_effect(:highlight, 'question-list')  
%>

<style>

</style>

<script>
  function heighlight(el){
    new Effect.Highlight(el, {duration: 1.5})
  }
  
  if(null != "<%= @heighlight_id %>" && "" != "<%= @heighlight_id %>") {
    heighlight("item_<%= @heighlight_id %>");
  }

  function hideIt() {
    <% if @hide_id %>
      alert(<%=@hide_id%>);
      new Effect.Puff("<%= @hide_id %>",{duration: 1.5})
    <% end %>
  }

//异步删除
function deleteQuestion(id) {
  alert(1);
    new Ajax.Updater(
      "questionsDiv",
      "destroy_question",
      {
        method: "delete",
        asynchronous: true,
        evalScripts: true,
        parameters: Form.serialize(form) + '&authenticity_token=' + window._token + "&id=" + id
      }
    );
}


//异步修改题目
function updateQuestion(form, questionId, showEl, hideEl) {
    showEl.hide();
    hideEl.show();
    new Ajax.Updater(
      showEl,
      form.action + "/" + questionId,
      {
        method: "post",
        onSuccess: showQuestion(showEl, hideEl),
        asynchronous: true,
        evalScripts: true,
        parameters: Form.serialize(form) + '&authenticity_token=' + window._token
      }
    );
}
function destroy_choice(c_id,t_id)
{
new Ajax.Updater(
    "",
    "/surveys/prof_surveys/destroy_choice/"+c_id+"$"+t_id,
    {  method: 'get',
      asynchronous: true,
      evalScripts: true,
      parameters: 'authenticity_token=' + window._token  
    }
  );
}
var showQuestion = function(showEl, hideEl) {
  hideEl.hide();
  showEl.show();
}
</script>

